﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for AdminDatabase.xaml
    /// </summary>
    public partial class AdminDatabase : UserControl
    {
        private Settings _s;
        public AdminDatabase()
        {
            InitializeComponent();
            DBExportPathTextBox.Text = Properties.Settings.Default.DBExportPath;
            DBConnectionTextBox.Text = Properties.Settings.Default.DBConnectionString;
        }
        public AdminDatabase(Settings s)
        {
            InitializeComponent();
            DBExportPathTextBox.Text = Properties.Settings.Default.DBExportPath;
            DBConnectionTextBox.Text = Properties.Settings.Default.DBConnectionString;
            _s = s;
        }



        private async void DestroyDBClicked(object sender, RoutedEventArgs e)
        {
            if (DBPasswordContent.Visibility == Visibility.Collapsed)
            {
                DBPasswordContent.Visibility = Visibility.Visible;
                DestroyDBPasswordBox.Focus();
            }
            else
            {
                string user = await UsersController.GetInstance().GetLoggedUserID();
                bool validate = await UsersController.GetInstance().ValidateUserCredentials(user, DestroyDBPasswordBox.Password);
                if(validate == true){
                    DBPasswordContent.Visibility = Visibility.Collapsed;
                  await  DatabaseController.GetInstance().DestroyDatabase(1);
                    DestroyTile.Click -= DestroyDBClicked;
                    DestroyDBCountDownLabel.FontSize = 20;
                    for (int i = 60;i >0; i--)
                    {
                        await Task.Delay(1000);
                        DestroyDBCountDownLabel.Content = "" + i;
                    }
                      await Task.Delay(1000);
                      DestroyDBCountDownLabel.Content = "DB Destroyed";

                }else{

                }
            }
        }

        private async void AdminDBTab_Loaded(object sender, RoutedEventArgs e)
        {
            TotalSensorsCount.Content = "" + await Task.Run(() => { return DatabaseController.GetInstance().GetSensorsCount(); });
            TotalsRulesCount.Content = "" + await Task.Run(() => { return DatabaseController.GetInstance().GetRulesCount(); });
            TotalUsersCount.Content = "" + await Task.Run(() => { return DatabaseController.GetInstance().GetUsersCount(); });
            DatabaseSize.Content ="" +(await Task.Run(() => { return DatabaseController.GetInstance().GetDatabaseSize(); }))+" MB";
        }

        private void SaveDestinationButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
            folderDialog.SelectedPath = "C:\\";

            System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();
            if (result.ToString() == "OK")
            {

                Properties.Settings.Default.DBExportPath = folderDialog.SelectedPath;
                Properties.Settings.Default.Save();
            }
            DBExportPathTextBox.Text = Properties.Settings.Default.DBExportPath;
        }

        private void SaveConnectionPathButton_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.DBConnectionString = DBConnectionTextBox.Text;
            Properties.Settings.Default.Save();
        }

        private async void ExportButtonTile_Click(object sender, RoutedEventArgs e)
        {
            SavingAnimation.IsActive = true;
            bool result = await Task.Run(()=>{ return DatabaseController.GetInstance().BackupDatabase(Properties.Settings.Default.DBExportPath);});
            SavingAnimation.IsActive = false;
        }

        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                DestroyDBClicked(sender, null);
            }
        }

        private void DBMenuHandler(object sender, MouseButtonEventArgs e)
        {
            DBMenu.IsOpen = !DBMenu.IsOpen;
        }

        public void DatabaseMenuBack(object sender, RoutedEventArgs e)
        {
            DBMenu.IsOpen = false;
            _s.OpenAdminSettings();
          //  _s.DatabaseMenuBack(sender, e);
        }
    }
}
